﻿//Mirco Töpfer

using System;
using System.Collections.Generic;
using pheidippides.logging.Interfaces;

namespace pheidippides.logging.Classes
{
    public class Logger : ILogger
    {
        #region Singleton Kram

        private static ILogger _staticILogger;

        public static ILogger GetInstance()
        {
            if (_staticILogger == null)
                throw new ApplicationException("The Logger is not initialized");
            return _staticILogger;
        }

        public static void Initialize(ILogger loggingInstance)
        {
            _staticILogger = new Logger(loggingInstance);
        }

        private Logger(ILogger loggingInstance)
        {
            _loggingInstance = loggingInstance;
            loggingInstance.OnNewLogEntry += LoggingInstance_OnNewLogEntry;
        }

        #endregion

        #region Private Members

        private readonly ILogger _loggingInstance;

        #endregion

        #region Public Methods

        public void LogMessage(string message, LogLevel logLevel)
        {
            _loggingInstance.LogMessage(message, logLevel);
        }

        public void LogException(string message, Exception ex, LogLevel logLevel)
        {
            _loggingInstance.LogException(message, ex, logLevel);
        }

        public void ChangeLogLevel(LogLevel logLevel)
        {
            _loggingInstance.ChangeLogLevel(logLevel);
        }

        public List<LogEntry> GetLogEntrys()
        {
            return _loggingInstance.GetLogEntrys();
        }

        public string GetLastLogEntrysString()
        {
            return _loggingInstance.GetLastLogEntrysString();
        }

        #endregion

        #region Events

        public event NewLogEntry OnNewLogEntry;

        #endregion

        #region Private Methods

        private void LoggingInstance_OnNewLogEntry(LogEntry logEntry)
        {
            if (OnNewLogEntry != null)
                OnNewLogEntry(logEntry);
        }

        #endregion
    }
}